package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.CollectionBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import com.mongodb.client.result.DeleteResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@Service
@Slf4j
public class ApCollectionBehaviorServiceImpl implements ApCollectionBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public ResponseResult collectBehavior(CollectionBehaviorDTO dto) {
        if (StringUtils.isEmpty(dto.getArticleId())){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"文章id为空");
        }
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"未登录，请先登录");
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), null);
        Query query = Query.query(Criteria.where("articleId").is(dto.getArticleId()).and("entryId").is(apBehaviorEntry.getId()));
        ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
        log.info("查询收藏行为是否存在" + apCollection);
        if (apCollection==null){
            if (dto.getOperation()==1){
                CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"请先收藏");
            }
            ApCollection apLikesBehavior2 = ApCollection.builder()
                    .entryId(apBehaviorEntry.getId())
                    .articleId(dto.getArticleId())
                    .type((short)dto.getType())
                    .build();
            ApCollection saveResult = mongoTemplate.insert(apLikesBehavior2);
            return ResponseResult.okResult(saveResult);
        }else{
            if (dto.getOperation()==1){
                DeleteResult removeResult = mongoTemplate.remove(query, ApCollection.class);
                log.info("用户取消收藏成功"+removeResult);
                return ResponseResult.okResult(removeResult);
            }else{
                CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"请勿重复收藏");
            }
        }
        return null;
    }
}
